//
// Created by 17775 on 2024/10/18.
//

#ifndef RAYCI_BEAM_STABILITY_H
#define RAYCI_BEAM_STABILITY_H

#include "RayCiPro_C.h"
#include <vector>
#include <thread>
#include <atomic>
#include <QString>
#include <QObject>
#include <QDebug>
#include <windows.h>
#include <shellapi.h>

struct BeamStabilityDataBatch {
    std::vector<float> peakRelativeTime, peakValue;
    std::vector<float> powerRelativeTime, powerValue;
    std::vector<float> cxRelativeTime, cxValue;
    std::vector<float> cyRelativeTime, cyValue;
    std::vector<float> majorRelativeTime, majorValue;
    std::vector<float> minorRelativeTime, minorValue;
    std::vector<float> angleRelativeTime, angleValue;
    double peakMin = 0, peakMax = 0, peakAverage = 0, peakStd = 0;
    double powerMin = 0, powerMax = 0, powerAverage = 0, powerStd = 0;
    double centroidXMin = 0, centroidXMax = 0, centroidXAverage = 0, centroidXStd = 0;
    double centroidYMin = 0, centroidYMax = 0, centroidYAverage = 0, centroidYStd = 0;
    double majorMin = 0, majorMax = 0, majorAverage = 0, majorStd = 0;
    double minorMin = 0, minorMax = 0, minorAverage = 0, minorStd = 0;
    double angleMin = 0, angleMax = 0, angleAverage = 0, angleStd = 0;
};

class Beam_Stability : public QObject {
Q_OBJECT

signals:
    void getBeamStabilityData(const BeamStabilityDataBatch& data);
    void beamStabilityMeasurementOver();

public slots:
    void exportReport();

public:
    ~Beam_Stability();
    int _iBeamStabilityDocId = 0;
    void stopBeamStability();
    void stopBeamStabilityThread();
    void clearBeamStability();
    void setFrameSpan(int frames);
    void setEveryFrame(bool bEveryFrame);
    void setTimeSpan(double dTime);
    void setTimeStep(double dStep);
    void setFreeRunning(bool freeRunning);

    std::thread m_beamStabilityThread;
    std::atomic<bool> getBeamStabilityDataThreadRunning = true;
    void getData();
    void getAllData();
    void getChartData(int nChartType);
    QString getDataUnit(int nChartType);
    double getStatisticalResult(int nChartType, int nColumn);

    void close(bool bWaitWhileClosing);
    void closeAll(bool bWaitWhileClosing);
};


#endif //RAYCI_BEAM_STABILITY_H
